Delphi Live Templates
Delphi 2006 introduced a feature called "Live Templates" that allows one to write macro type templates that expand from a keyword and are parameterized, i.e. you can tab through the parameters and supply them one by one. This is a great productivity aid. The best part is that you can provide your own and it is relatively simple to do so. How to use live templates To use these "live templates," all you need to do is copy the XML snippet provided and save it in D2006: c:\Program Files\Borland\BDS\4.0\ObjRepos\Code_Templates\Delphi\ D2009: c:\Program Files\CodeGear\RAD Studio\6.0\ObjRepos\Code_Templates\Delphi\ with an xml extension. Alternate places to put them are D2006: c:\Documents and Settings\\Local Settings\Application Data\Borland\BDS\4.0\code_templates\ D2009: c:\Users\\Documents\RAD Studio\code_templates\ Note that you need to restart BDS if you just copy the file. Alternatively you can use the IDE to create a new template, delete all the automatically filled in stuff, copy and paste the template from below and use the IDE to save it. In that case the template will be immediately available. A suggested file name is provided above the snippet but you can use whatever you like. Live Templates for Developers Coming from VB MsgBox Just typing "msgbox" following by a space (or tab) will invoke this live template and generate the Delphi equivalent (with the ability to tab between the parameters you need to supply): MessageDlg('Hello', mtInformation, mbOK, 0); This script was adapted from Bob Swart's blog posting. TEMPLATE FILE NAME: vb_live_template_msgbox.xml MessageDlg live template Bob Swart Hello Message mtInformation Message Type mbOK Message Buttons MessageDlg('|Message|', |MessageType|, [|MessageButtons|], 0);|end| General Language Templates TODO This template will create a TODO item in your source TEMPLATE FILE NAME: todo.xml A live template to create a TODO item Bill Mullen Owner Enter the person responsible for this item General Enter the category for this item ActionItem Enter the action to be taken {TODO -o|Owner| -c|Category| : |ActionItem|} Database Live Templates EOF Here is a live template that automatically creates the following block of code (and allows you to change the dataset name) quickly. You simply type "eof" and hit and the code block appears, positions you to change the name of "dataset", changes the name both places it's referenced, then places the cursor at the top of the new begin block. while not dataset.eof do begin dataset.Next; end; TEMPLATE FILE NAME: not_eof.xml dataset dataset to loop through Template to loop through a dataset while not eof Jeremy D. Mullin while not |dataset|.eof do begin |*||end| |*||dataset|.Next; end; Live Templates for Developers used to CodeRush FreeAndNil Typing F translates into FreeAndNil(Variable). With the help of the LiveTemplate ScriptEngine from Adam Markowitz (see CodeCentral ID 23910) the text of the first point defaults to the Clipboard content. This template also works without this ScriptEngine installed. TEMPLATE FILE NAME: cr_live_template_freeandnil.xml variable variable to be freed FreeAndNil Sebastian Modersohn FreeAndNil(|variable|); i: Integer Declaration Typing i: results in i: Integer. Useful for declaring i: Integer variable. TEMPLATE FILE NAME: cr_live_template_integerdecl.xml AutoComplete i: Integer declaration Sebastian Modersohn i: Integer; Alternatively, if you want to use it anywhere but have your variable declared in the correct spot: AutoComplete i: Integer declaration Sebastian Modersohn / Thomas Mueller i Integer |ident||type| s: String Declaration Typing s: results in s: String. Useful for declaring s: String variable. TEMPLATE FILE NAME: cr_live_template_stringdecl.xml AutoComplete s: String declaration Sebastian Modersohn s: String; convert typo ";=" to ":=" This will convert a common typo where you type ";=" but really meant ":=" to what you meant. TEMPLATE FILE NAME: semicolon-equals-typo.xml convert typo ;= to := twm := |end| Property that refers to a field Those two templates create a property declaration that refers to a field. Press Ctrl-Shift-C afterwards to have the IDE add the fields. Readonly property This will create a readonly property that refers to an object's field. TEMPLATE FILE NAME: propfreadonly.xml read only property for field twm Name the name for the property Integer the type for the property property |ident|: |type| read F|ident|; |end| Read/write property This will create a read / write property that refers to an object's field. TEMPLATE FILE NAME: propfreadwrite.xml read write property for field twm Name the name for the property Integer the type for the property property |ident|: |type| read F|ident| write F|ident|; |end| Getter, Setter or Field This will create a read / write property that refers to either Get/Set methods or an object's field depending on what changes you make once invoked using propgs and either tab or space in the IDE. TEMPLATE FILE NAME: PropertyGetSet.xml Property with Getter and Setter. twm-tdh Name the Name for the property Integer the Type for the property Get the property Getter Set the property Setter property |Name|: |Type| read |Getter||Name| write |Setter||Name|;|end| Execute class method for a form Generates a class method for a form that creates the form, shows it modally and returns true, if OK was pressed. Press Shift-Ctrl-C afterwards to add the method's declaration. Adding the script Tag with InvokeClassCompletion (see below) makes it unnecessary to press the Shift-Ctrl-C afterwards TEMPLATE FILE NAME: FormExecute.xml creates an Execute class method for a form, press Shift-Ctrl afterwards to add the method declaration. twm TForm name of the form class _Value value edited with this form string Type of value Edit1 control that edits the value Text property of the control for editing class function |FormClass|.Execute(_Owner: TComponent; var |Value|: |Type|): boolean; var frm: |FormClass|; begin frm := |FormClass|.Create(_Owner); try frm.|control|.|property| := |Value|;|end| Result := (frm.ShowModal = mrOK); if Result then begin |Value| := frm.|control|.|property|; end; finally frm.Free; end; end; Como assim? Live Templates for Pseudo Templates On https://osdn.net/projects/dzlib-tools/ you can find "pseudo templates". "Pseudo templates" are actually more like C++'s templates or C# and Java's generic types. In the 1.0.1 release some Live Templates are included that make using these pseudo templates easier. Improvements for existing templates forin The forin template always declares the iterator variable as TObject (QC 36085). Here is a forint template which has an entry point for the type too. Unfortunately the type stays in the code and must be removed manually (there is only RemoveTemplate as a script command available, but that would clear everything). MyElem collection element iterator variable MyElemType type of iterator variable MyList collection to iterate for in loop Sebastian Modersohn for |ident|: |type| in |collection| do begin |selected||*||end| end; Troubleshooting Tips * The language attribute is case-sensitive. "delphi" doesn't work, but "Delphi" does. More Information on Live Templates See Live Templates Technical Info See Editing the template_template Simon Stuart had a Live Template Editor project called RADPlates but unfortunately it's no longer available. a big collection of Live Templates Category:References